var ACTION = require("../../base/action");
var common = require("./common");
var utils = require("../../utils/utils");
var ocrGml = require("../../utils/ocrGml");
var aTreasure = new ACTION("开宝箱", {
  storage: common.storage,
});

aTreasure.start = function () {
  var content = "点击领金币";
  var UIText = utils.existsTextStartsWith(content);
  if (UIText) {
    utils.clickUIText(UIText);
    sleep(utils.getRandom(2.5, 4.5, 1000));
    return true;
  }
  var OcrItem = ocrGml.captureExistOne(content, true);
  if (OcrItem) {
    utils.clickOcrItem(OcrItem);
    sleep(utils.getRandom(2.5, 4.5, 1000));
    return true;
  }
  return false;
};
aTreasure.watchAd = function () {
  sleep(utils.getRandom(2.5, 4.5, 1000));
  common.ad.watchAd();
  var content = "开心收下";
  var UIText = utils.existsTextStartsWith(content);
  if (UIText) {
    utils.clickUIText(UIText);
    var waitSpan = utils.getRandom(2.5, 3, 1000);
    console.log("广告看完，再等待-" + waitSpan / 1000 + "-后继续");
    sleep(waitSpan);
    return;
  }
  var OcrItem = ocrGml.captureExistOne(content);
  if (OcrItem) {
    utils.clickOcrItem(OcrItem);
    var waitSpan = utils.getRandom(2.5, 3, 1000);
    console.log("广告看完，再等待-" + waitSpan / 1000 + "-后继续");
    sleep(waitSpan);
    return;
  }
};
aTreasure.exec = function () {
  var execCount = 0;
  while (this.start()) {
    execCount++;
    if (execCount == 20) {
      console.log("aTreasure.exec 执行次数超过限制");
    }
    var content = "看广告视频再得";
    var UIText = utils.existsTextStartsWith(content);
    if (UIText) {
      utils.clickUIText(UIText);
      this.watchAd();
      continue;
    }
    var OcrItem = ocrGml.captureExistOne(content, true);
    if (OcrItem) {
      utils.clickOcrItem(OcrItem);
      this.watchAd();
      continue;
    }
  }
};
module.exports = aTreasure;
